
$(function(){
	
	var baseUrl = $("base").attr("href");
	var isEnd = false;
	
	var loadDataDefault = {
				url : baseUrl + "user/json-showPersonPublickInfo.do",
				data:{pageSize:5,pageNum:1,statue : "发布中"},
				curPage : 1,
			    pageSize : 5,
	            maxSize : 0,
	}
				
	
	var $_modifyNode;
	
	
	/** 请求数据列表显示在table中 开始  */
	
	function loadData(){
		$.ajax({
		type:"post",
		url: loadDataDefault.url,
		async:true,
		data:loadDataDefault.data,
		success:function(data){
			if(data.code == 1000){
				$("section table tbody").html("");
				console.log("estateInfoList",data);
				estatInfos = JSON.parse(data.data);
				var i=0;
				if(!isEnd && estatInfos.length > 0){
					loadDataDefault.maxSize = loadDataDefault.curPage;
					if(estatInfos.length < loadDataDefault.pageSize){
						isEnd = true;
					}
				}else{
					isEnd = true;
					
				}
				for ( i; i < estatInfos.length; i++) {
					var item = estatInfos[i];
					var htmlStr =   
					"<tr >"+
					"	<td>" + item.id + "</td>"+
					"	<td>"+ item.estatebasicinfo.user.realName +"</td>"+
					"	<td>"+ new Date(item.publishTime).toLocaleDateString()+"</td>"+
					"	<td>"+ item.minPrice + "&nbsp;&nbsp;/<span>"+ item.unit +"</span></td>"+
					"	<td class=\"btn-group\">"+
					"	<button class=\"btn btn-default\">房产信息</button>"+
				    "	<button class=\"btn btn-info\">发布详情</button>"+
					"	<button class=\"btn btn-success\">修改</button>"+
					"	<button class=\"btn btn-danger\">取消发布</button>"+
					"	</td>"+
					"	</tr>";
					$(htmlStr).appendTo($("section tbody")).data("publicInfo",item);
				}
			}else{
				console.log("loadDataError",data);
				sweetAlert({
				title:"错误",
				text:"系统错误",
				type:"error",
				timer:2000,
			});
			}
		},
		error:function(){
			sweetAlert({
				title:"错误",
				text:"连接服务器请求数据失败",
				type:"error",
				timer:2000,
			});
			
		}
		
	});
	}
	
	/** 请求数据列表显示在table中 结束  */
	
	/* 动态html事件绑定  开始*/
	
	//显示房产信息
	$("section table").on("click",".btn-default",function(){
		var basicInfo = $(this).parents("tr").data("publicInfo").estatebasicinfo;
		console.log("publicInfo",basicInfo);
		$(".PublicInfoID").eq(0).text(basicInfo.id);
		$("#owner").text(basicInfo.user.realName);
		$("#email").text(basicInfo.user.email);
		$("#tel").text(basicInfo.user.tel);
		$("#certificateId").text(basicInfo.certificateId);
		$("#certificateType").text(basicInfo.certificateType);
		$("#estateAddress").text(basicInfo.estateAddress);
		$("#estateType").text(basicInfo.estateType);
		$("#estateUsingStatue").text(basicInfo.estateUsingStatue);
		$("#structure").text(basicInfo.structure);
		$("#acreage").text(basicInfo.acreage);
		$("#estateDtail").modal("show");
		
	});
	
	//发布详情
	$("section table").on("click",".btn-info",function(){
		var publicInfo = $(this).parents("tr").data("publicInfo");
		$(".PublicInfoID").eq(1).text(publicInfo.id);
		$("#maxScheduleAmount").text(publicInfo.maxScheduleAmount);
		$("#minPrice").text(publicInfo.minPrice);
		$("#unit").text(publicInfo.unit);
		$("#publishTime").text(publicInfo.publishTime);
		$("#publicDescribe").text(publicInfo.publicDescribe);
		$("#PublicInfo").modal("show");
		
	});
	
	//修改发布信息
	$("section table").on("click",".btn-success",function(){
		var publicInfo = $(this).parents("tr").data("publicInfo");
		$(".PublicInfoID").eq(2).text(publicInfo.id);
		$("input[name=id]").val(publicInfo.id);
		$("select[name=maxScheduleAmount]").val(publicInfo.maxScheduleAmount);
		$("input[name=minPrice]").val(publicInfo.minPrice);
		$("select[name=unit]").val(publicInfo.unit);
		$("textarea[name=publicDescribe]").val(publicInfo.publicDescribe);
		$("#estatePublic").modal("show");
	});
	
	
	
	
	$("section table").on("click",".btn-danger",function(){
		var publicInfo = $(this).parents("tr").data("publicInfo");
		$_modifyNode = $(this).parents("tr");
		sweetAlert({
			title:"警告",
			text:"您确定要取消本条信息的发布吗?",
			type:"warning",
			showCancelButton: true,   
			confirmButtonColor: "#DD6B55", 
			confirmButtonText: "是的, 我要取消",
			cancelButtonText: "不, 保留发布",
			closeOnConfirm: false,
			closeOnCancel: true,
			
		},function(isConfirm){
			if(isConfirm){
				$.ajax({
					url: baseUrl + "user/json-cancelPublic.do",
					method: "post",
					data : {id:publicInfo.id},
					success:function(data){
						if(data.code == 1000){
							sweetAlert({
								title:"成功",
								text:"本条记录已删除",
								tytp:"success",
								timer:2000,
							});
						$_modifyNode.remove();
						}else{
							console.log("delet error", data)
							sweetAlert({
								title:"错误",
								text:"系统上错",
								tytp:"error",
								timer:2000,
							});
						}
					},
					error:function(){
						sweetAlert({
								title:"错误",
								text:"连接服务器失败",
								tytp:"error",
								timer:2000,
							});
					}
				});
			}
		});
		
	});
	/* 动态html事件绑定  开始*/
	
	/* 翻页  开始 */
	$(".previous").click(function(){
		if(loadDataDefault.curPage > 1){
			loadDataDefault.curPage--;
			loadDataDefault.data.pageNum = loadDataDefault.curPage;
			loadData();
			if(loadDataDefault.curPage == 1){
				$(this).addClass("disabled");
			}
			if(isEnd && loadDataDefault.curPage == (loadDataDefault.maxSize-1)){
				$(".next").removeClass("disabled");
			}
			
		}
	});
	
	$(".next").click(function(){
		loadDataDefault.curPage++;
		if(isEnd && loadDataDefault.curPage >= loadDataDefault.maxSize){
			loadDataDefault.curPage = loadDataDefault.maxSize;
			$(this).addClass("disabled");
		}
		loadDataDefault.data.pageNum = loadDataDefault.curPage;
		loadData();
		if(loadDataDefault.curPage == 2){
			$(".previous").removeClass("disabled");
		}
	});
	
	/* 翻页  结束 */
	
	/* 修改提交 开始 */
	$("#modify-submit").click(function(){
		
		$.ajax({
			method:"post",
			url: baseUrl + "user/json-modifyPublicInfo.do",
			data:fromToJson("public-form"),
			success:function(data){
				if(data.code == 1000){
					sweetAlert({
						title:"成功",
						text:"审核信息提交成功",
						type:"success",
						timer:2000,
					});
				}else{
					console.log("loadDataError",data);
					sweetAlert({
						title:"错误",
						text:"系统错误",
						type:"error",
						timer:2000,
					});
				}
			},
			error:function(){
				sweetAlert({
					title:"错误",
					text:"连接服务器请求数据失败",
					type:"error",
					timer:2000,
				});
			}
		});
		
		$("#estatePublic").modal("hide");
	});

	/* 修改提交 结束 */
	
	
	
	//从from获取数据，转为对象  
	function fromToJson(form) {  
	    var result = {};  
	    var fieldArray = $('#' + form).serializeArray();  
	    for (var i = 0; i < fieldArray.length; i++) {  
	        var field = fieldArray[i];  
	        if (field.name in result) {  
	            result[field.name] += ',' + field.value;  
	        } else {  
	            result[field.name] = field.value;  
	        }  
	    }  
	    return result;  
}  
	
	
	/* 开始操作 */
	
	loadData();
	
});